package com.honeycomb.action.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.honeycomb.action.BaseWebAction;
import com.honeycomb.service.IUserService;
import com.honeycomb.utils.MD5Utils;
import com.honeycomb.utils.SystemConstants;
import com.honeycomb.vo.BaseVO;
import com.honeycomb.vo.ResultDto;
import com.honeycomb.vo.UserInfoVO;
import com.honeycomb.vo.UserPasswordVO;
import com.honeycomb.vo.UserRegistVO;
import com.honeycomb.vo.UserStatusVO;

/**
 * 
 * @function: 用户登录
 * 
 * @param userLoginEmail 用户ID
 * 
 * @Param userLoginPassword 用户密码
 * 
 * @return String
 * 
 * @author ZhongJiang
 *
 */
public class DoLoginAction extends BaseWebAction {
	private IUserService iUserService;
	
	private String userLoginEmail;
	private String userLoginPassword;
	
	public String getUserLoginEmail() {
		return userLoginEmail;
	}
	public void setUserLoginEmail(String userLoginEmail) {
		this.userLoginEmail = userLoginEmail;
	}
	public String getUserLoginPassword() {
		return userLoginPassword;
	}
	public void setUserLoginPassword(String userLoginPassword) {
		this.userLoginPassword = userLoginPassword;
	}
	public IUserService getiUserService() {
		return iUserService;
	}
	public void setiUserService(IUserService iUserService) {
		this.iUserService = iUserService;
	}

	@Override
	protected String doRequest(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {
		logger.info("---------------------DoLoginAction------------------------");
		
		logger.info("userLoginEmail:"+this.getUserLoginEmail());
		logger.info("userLoginPassword:"+this.getUserLoginPassword());
		
		UserPasswordVO UserPasswordVO = new UserPasswordVO();
		ResultDto<UserInfoVO> resultDtoLogin = null;
		UserStatusVO userStatusVO = new UserStatusVO();
		ResultDto<BaseVO> resultDtoUpdateLoginStatus = null;
		try {
			UserPasswordVO.setUserId(this.getUserLoginEmail());
			UserPasswordVO.setPasswordValue(MD5Utils.generateForPassword(this.getUserLoginPassword()));
			//作用户登录
			resultDtoLogin = this.getiUserService().doLogin(UserPasswordVO);
			if(resultDtoLogin.isSuccess()) {
				//作更新用户状态
				userStatusVO.setUserId(this.getUserLoginEmail());
				resultDtoUpdateLoginStatus = this.getiUserService().doUpdateUserStatusToOnline(userStatusVO);
				if(resultDtoUpdateLoginStatus.isSuccess()) {
					//做Session操作
					this.getSSN().setAttribute(SystemConstants.HoneyComb_LoginUser_Session_Name, resultDtoLogin.getFirstResult());
					return "success";
				}else {
					return "failed";
				}
			}else {
				return "failed";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "GlobalErrorPage";
		}
	}

}

















